package com.yyds.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyds.yygh.cmnclient.DictFeignClient;
import com.yyds.yygh.enums.DictEnum;
import com.yyds.yygh.model.user.Patient;
import com.yyds.yygh.user.mapper.PatientMapper;
import com.yyds.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllByUserId(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patients = patientMapper.selectList(queryWrapper);
        //对每个就诊人进行打包
        for (Patient patient : patients) {
            patient = this.patientPackage(patient);
        }
        return patients;
    }

    @Override
    public Patient getPatientById(Long id) {
        Patient patient = patientMapper.selectById(id);
        return this.patientPackage(patient);
    }
    //Patient对象里面其它参数封装
    private Patient patientPackage(Patient patient){
        //获取就诊人身份证类型
        String certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //获取联系人身份证类型
        String ContactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //获取省名
        String provinceName = dictFeignClient.getName(patient.getProvinceCode());
        //获取城市名
        String cityName = dictFeignClient.getName(patient.getCityCode());
        //获取区域名
        String districtName = dictFeignClient.getName(patient.getDistrictCode());
        //获取地名
        String address = patient.getAddress();

        patient.getParam().put("certificatesTypeString",certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString",ContactsCertificatesTypeString);
        patient.getParam().put("provinceName",provinceName);
        patient.getParam().put("cityName",cityName);
        patient.getParam().put("districtName",districtName);
        patient.getParam().put("fullAddress",provinceName+cityName+districtName+address);
        return patient;
    }
}
